<?php
namespace app\manage\model;

use think\Model;

class Menu extends Model
{
    //无限极分类
    public static $treeListEasy = array();
    public static function treeEasy($data,$pid = 0){
        foreach ($data as $key => $value) {
            if($value['parentid'] == $pid){
                self::$treeListEasy[] = $value;
                self::treeEasy($data,$value['id']);
            }
        }
        return self::$treeListEasy;
    }

    //无限极分类
    public static $treeListTest = array();
    public static $parentid = 0;
    public static $sparentid = 0;
    public static function treeTest($data,$pid = 0,$level = 1){
        foreach ($data as $key => $value) {
            if($value['parentid'] == $pid){
                if($level == 1){
                    self::$parentid = $value['id'];
                    self::$treeListTest[self::$parentid] = $value;
                }else if($level == 2){
                    self::$sparentid = $value['id'];
                    self::$treeListTest[self::$parentid]['son'][self::$sparentid] = $value;
                }else if($level == 3){
                    self::$treeListTest[self::$parentid]['son'][self::$sparentid]['son'][] = $value;
                }
                if($value['child']){
                    self::treeTest($data,$value['id'],$level+1);
                }
            }
        }
        return self::$treeListTest;
    }

    //无限极分类
    public static function tree($data){
        $tree = [];
        //将菜单项id作为数组下标
        foreach ($data as $key => $value) {
            $tree[$value['id']] = $value;
        }

        //利用引用，将子菜单添加到父类son中，只需遍历一次
        foreach ($tree as $key => $value)
            $tree[$value['parentid']]['son'][] = &$tree[$key];
        $tree = isset($tree[0]['son']) ? $tree[0]['son'] : array();

        //删除重复数据
        // foreach ($tree as $key => $value) {
        //   if($value['pid'] !=0){
        //     unset($tree[$key]);
        //   }
        // }

        return $tree;
    }











}
